import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_10: 分类歌单详情页面的专辑可正常播放')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001043370?url_cache_key=17e362af8b12add5e45c5e468a11c15a')
class TestCaseMusic10:
    """
    预期结果: 标题栏文字进行跑马灯效果展示，同时在缩略播放器中显示播放状态
    """

    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()

        self.music_choice_page = CommonPage(self.driver).get_music_choice_page()
        self.music_index_page = CommonPage(self.driver).get_music_index_page()
        self.music_category_page = CommonPage(self.driver).get_music_category_page()
        self.music_category_detail_page = CommonPage(self.driver).get_music_category_detail_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('music')
        self.driver.quit()

    @allure.story("step1: 进入分类详情页面")
    @pytest.mark.run(order=101)
    def test_music_10_1_enter_category_detail(self):
        try:
            # 进入音乐
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)
            if self.music_choice_page.is_display_title():
                self.music_choice_page.click_skip()

            self.music_index_page.click_category()
            self.music_category_page.click_list_tag_text()
            time.sleep(2)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 点击列表音乐的任意封面, 检查标题跑马灯")
    @pytest.mark.run(order=102)
    def test_music_10_2_click_list_music_img(self):
        try:
            self.music_category_detail_page.screenshot_part(self.music_category_detail_page.get_list_music_title(1),
                                                            'category_img1')
            self.music_category_detail_page.click_list_music(1)
            time.sleep(2)
            self.music_category_detail_page.screenshot_part(self.music_category_detail_page.get_list_music_title(1),
                                                            'category_img2')
            contrast_res = self.music_index_page.image_contrast('category_img1.png', 'category_img2.png')
            assert contrast_res > 0
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            file1 = open('Image/category_img1.png', 'rb').read()
            allure.attach(file1, '播放前的标题', allure.attachment_type.PNG)
            file2 = open('Image/category_img2.png', 'rb').read()
            allure.attach(file2, '播放后的标题', allure.attachment_type.PNG)

    @allure.story("step3: 判断缩略播放器的播放状态")
    @pytest.mark.run(order=103)
    def test_music_10_3_is_playing(self):
        try:
            self.music_index_page.click_thumb_icon()
            self.music_index_page.screenshot_part(self.music_index_page.get_thumb_icon(), 'category_icon_img1')
            self.music_index_page.click_thumb_icon()
            self.music_index_page.screenshot_part(self.music_index_page.get_thumb_icon(), 'category_icon_img2')
            contrast_res = self.music_index_page.image_contrast("category_icon_img1.png", "category_icon_img2.png")
            assert contrast_res > 0
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            file1 = open('Image/category_icon_img1.png', 'rb').read()
            allure.attach(file1, '暂停时的icon', allure.attachment_type.PNG)
            file2 = open('Image/category_icon_img2.png', 'rb').read()
            allure.attach(file2, '播放时的icon', allure.attachment_type.PNG)


if __name__ == '__main__':
    pytest.main("-s testcase_10.py")
    # pass
